package ar.com.fiuba.ElectronicCircuitAnalyst.infrastructure.db4o.repository;

import java.util.ArrayList;

import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Component;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Lead;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.LogicalFunction;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Output;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.repository.ComponentRepository;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.exceptions.CircuitElementNotFoundException;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.exceptions.InvalidCircuitElementException;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.impl.ComponentImpl;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.specs.ComponentDummySpecification;

import com.db4o.ObjectSet;

public class ComponentRepositoryImpl extends RepositoryImpl<Component> implements ComponentRepository {

	public ComponentRepositoryImpl() {
		super(Component.class);
	}

	public Component getComponentByName(String name) throws CircuitElementNotFoundException {
		Component component = null;
		try {
			component = new ComponentImpl(name, null, new ArrayList<Lead>(), new ArrayList<Output>(),
					new ArrayList<LogicalFunction>(), new ComponentDummySpecification());
		} catch (InvalidCircuitElementException e) {
			// Yet another unreachable block of code...
		}
		ObjectSet<Component> result = getDb().queryByExample(component);

		if (result.size() == 0) {
			throw new CircuitElementNotFoundException("No Circuit found with the given name: " + name);
		}
		return result.get(0);
	}

}
